function AppAssistant() {
	this.stages = {
		first: { name: 'cardStage', scene: 'first', type: 'card', assistant: 'StageAssistant' },
		status: { name: 'cardStage', scene: 'status', type: 'card', assistant: 'StageAssistant' },
		dashboard:  { name: 'dashStage', scene: 'dash', type: 'dashboard' }
	};

	this.cookie = new Mojo.Model.Cookie('creds');
}

AppAssistant.prototype.getClient = function() {
	return this.x;
};
AppAssistant.prototype.getCookie = function() {
	console.log('getCookie: ' + JSON.stringify(this.cookie.get()));
	return this.cookie.get();
};

AppAssistant.prototype.removeCookie = function() {
	console.log('removeCookie: ' + JSON.stringify(this.cookie.get()));
	this.cookie.remove();
	this.cookie = new Mojo.Model.Cookie('creds');
};
AppAssistant.prototype.setCookie = function(vals) {
	this.cookie.put(vals);
	console.log('setCookie: ' + JSON.stringify(vals));
};

AppAssistant.prototype.launchStage = function(stage) {
	var controller = this.controller;
	var proxy = controller.getStageProxy(stage.name);
	var stageController = controller.getStageController(stage.name);
	var stageArgs = { name: stage.name, lightweight: true, assistantName: stage.assistant};
	var pushSceneFunction = function(c) { c.pushScene(stage.scene); };
	if (proxy && controller) {
		console.log('launchStage: activating ' + stage.name);
		if (stageController) {
			stageController.swapScene(stage.scene);
		}
	} else {
		console.log('launchStage: creating ' + stage.name + ' as ' + stage.type);
		controller.createStageWithCallback(stageArgs,pushSceneFunction, stage.type);
	}
};

AppAssistant.prototype.setup = function() {
	console.log('setup');
	this.x = jQuery('<p/>').xmpp();
	this.x.xmpp('option','controller', this.controller);
};

AppAssistant.prototype.handleLaunch = function(params) {
	var source = params && params.source, initialized = this.x.xmpp('state').initialized;
	console.log('handleLaunch: source:' + JSON.stringify(params));
	if (source == 'stage-cleanup') {
		this.launchStage(this.stages.dashboard);
	} else {
		if (this.getCookie()) {
			this.launchStage(this.stages.status);
		} else {
			this.launchStage(this.stages.first);
		}
	}

	if (!initialized && this.getCookie()) {
		console.log('handleLaunch: connecting with xmpp');
		this.x.xmpp('option', this.getCookie());
		this.x.xmpp('connect');
	}

};
